package com.ruoyi.webshop.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.webshop.domain.GoodsPurchaseDetail;
import com.ruoyi.webshop.mapper.GoodsPurchaseDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.webshop.mapper.GoodsPurchaseMapper;
import com.ruoyi.webshop.domain.GoodsPurchase;
import com.ruoyi.webshop.service.IGoodsPurchaseService;

/**
 * 采购订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-17
 */
@Service
public class GoodsPurchaseServiceImpl implements IGoodsPurchaseService {
    @Autowired
    private GoodsPurchaseMapper goodsPurchaseMapper;

    @Autowired
    private GoodsPurchaseDetailMapper goodsPurchaseDetailMapper;

    /**
     * 查询采购订单
     *
     * @param purchaseId 采购订单主键
     * @return 采购订单
     */
    @Override
    public GoodsPurchase selectGoodsPurchaseByPurchaseId(Long purchaseId) {
        return goodsPurchaseMapper.selectGoodsPurchaseByPurchaseId(purchaseId);
    }

    /**
     * 查询采购订单列表
     *
     * @param goodsPurchase 采购订单
     * @return 采购订单
     */
    @Override
    public List<GoodsPurchase> selectGoodsPurchaseList(GoodsPurchase goodsPurchase) {
        return goodsPurchaseMapper.selectGoodsPurchaseList(goodsPurchase);
    }

    /**
     * 新增采购订单
     *
     * @param goodsPurchase 采购订单
     * @return 结果
     */
    @Override
    public int insertGoodsPurchase(GoodsPurchase goodsPurchase) {
        goodsPurchase.setCreateTime(DateUtils.getNowDate());
        int inserted = goodsPurchaseMapper.insertGoodsPurchase(goodsPurchase);
        if (inserted > 0) {

            List<GoodsPurchaseDetail> goodsPurchaseDetailList = goodsPurchase.getGoodsPurchaseDetailList();
            if (goodsPurchaseDetailList != null && goodsPurchaseDetailList.size() > 0) {
                for (GoodsPurchaseDetail goodsPurchaseDetail : goodsPurchaseDetailList) {
                    goodsPurchaseDetail.setPurchaseId(goodsPurchase.getPurchaseId());
                    goodsPurchaseDetailMapper.insertGoodsPurchaseDetail(goodsPurchaseDetail);
                }
            }
        }
        return inserted;
    }

    /**
     * 修改采购订单
     *
     * @param goodsPurchase 采购订单
     * @return 结果
     */
    @Override
    public int updateGoodsPurchase(GoodsPurchase goodsPurchase) {
        goodsPurchase.setUpdateTime(DateUtils.getNowDate());
        int updated = goodsPurchaseMapper.updateGoodsPurchase(goodsPurchase);


        if (updated > 0) {
            //更新采购订单明细
            goodsPurchaseDetailMapper.deleteGoodsPurchaseDetailByPurchaseId(goodsPurchase.getPurchaseId());

            List<GoodsPurchaseDetail> goodsPurchaseDetailList = goodsPurchase.getGoodsPurchaseDetailList();
            if (goodsPurchaseDetailList != null && goodsPurchaseDetailList.size() > 0) {
                for (GoodsPurchaseDetail goodsPurchaseDetail : goodsPurchaseDetailList) {
                    goodsPurchaseDetail.setPurchaseId(goodsPurchase.getPurchaseId());
                    goodsPurchaseDetailMapper.insertGoodsPurchaseDetail(goodsPurchaseDetail);
                }
            }
        }

        return updated;
    }

    /**
     * 批量删除采购订单
     *
     * @param purchaseIds 需要删除的采购订单主键
     * @return 结果
     */
    @Override
    public int deleteGoodsPurchaseByPurchaseIds(Long[] purchaseIds) {


        List<GoodsPurchaseDetail> list = new ArrayList<>();

        for (Long purchaseId : purchaseIds) {
            list.addAll(goodsPurchaseDetailMapper.selectGoodsSkuInfoListByPurchaseId(purchaseId));
        }
        goodsPurchaseDetailMapper.deleteGoodsPurchaseDetailByPurchaseDetailIds(
                list.stream().map(GoodsPurchaseDetail::getPurchaseDetailId).toArray(Long[]::new));


        return goodsPurchaseMapper.deleteGoodsPurchaseByPurchaseIds(purchaseIds);
    }

    /**
     * 删除采购订单信息
     *
     * @param purchaseId 采购订单主键
     * @return 结果
     */
    @Override
    public int deleteGoodsPurchaseByPurchaseId(Long purchaseId) {
        goodsPurchaseDetailMapper.deleteGoodsPurchaseDetailByPurchaseId(purchaseId);
        return goodsPurchaseMapper.deleteGoodsPurchaseByPurchaseId(purchaseId);
    }
}
